<?php
/**
 * callback for accounting
 */
function accounting_home(){
	$s = "// TODO";
	
	return $s;
}

/**
 * 
 */
function accounting_types(){
	require_once("accounting.inc");

	$ret = Accounting::get_account_types();
	$s = theme("account_types",$ret);
	
	return $s;
}

/**
 * 
 * @param unknown_type $title
 */
function accounting_accts($title="%"){
	_preload_accounting();
	
	$ret = Accounting::get_matching_accounts($title);
	$s = theme("accounts",$ret);
	
	return $s;
}

function accounting_autocomplete_accts($title){
	_preload_accounting();

	$ret = array();
	if($title){
		$accts = Accounting::get_matching_accounts($title);
		foreach($accts as $acct){
			$ret["{$acct->title}"] = check_plain($acct->title);
		}
	}
	drupal_json($ret);
}

/**
 * 
 * @param unknown_type $account
 */
function accounting_acct_view($account){
	
}

/**
 * 
 * @param unknown_type $account
 */
function accounting_acct_edit($account){
	
}

///**
// * 
// */
//function accounting_acct_new(){
//	return drupal_get_form("accounting_acct_form");
//}

function accounting_acct_form(){
	_preload_accounting();
	
	$form = array();
	
	$form["details"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Details")
	);
	
	$types = Accounting::get_account_types();
	$options = array();
	foreach($types as $t){
		$options["$t->id"] = $t->title;
	}
	
	$form["details"]["type"] = array(
		"#type"=>"select",
		"#title"=>t("Type"),
		"#description"=>t("Account type"),
		"#options"=>$options,
		"#default_value"=>$options[0]->id,
		"#required"=>TRUE
	);
	
	$form["details"]["title"] = array(
		"#type"=>"textfield",
		"#title"=>t("Title"),
		"#description"=>t("Account title"),
		"#required"=>TRUE
	);
	
	$form["details"]["description"] = array(
		"#type"=>"textfield",
		"#title"=>t("Description"),
		"#description"=>t("Account description"),
		"#required"=>TRUE
	);
	
	$form["details"]["parent"] = array(
		"#type"=>"textfield",
		"#title"=>t("Parent Account"),
		"#description"=>t("Parent account title"),
		"#autocomplete_path"=>"accounting/accounts/autocomplete"
	);
	
	$form["submit"] = array(
		"#type"=>"submit",
		"#value"=>t("Save")
	);
	
	// TODO put in correct redirect url
//	$form["#redirect"] = "accounting/accounts";
	
	return $form;
}

function accounting_acct_form_validate($form,&$form_state){
	_preload_accounting();
	
	$title = $form_state["values"]["title"];
	if($title == ""){
		form_set_error("title",t("Please enter an account title."));
	} else {
		$test = Accounting::get_account_by_title($title);
		if(isset($test)){
			form_set_error("title",t("The account title you entered already exists."));
		}
	}
	
	if($form_state["values"]["description"] == ""){
		form_set_error("description",t("Please enter an account description"));
	}
	
	if($form_state["values"]["parent"] != "" && !Accounting::account_exists($form_state["values"]["parent"])){
		form_set_error("parent",t("Please select a valid parent account title."));
	}
}

function accounting_acct_form_submit($form,&$form_state){
	$title = $form_state["values"]["title"];
	$desc = $form_state["values"]["description"];
	$type = $form_state["values"]["type"];
	$parent = $form_state["values"]["parent"];

	_preload_accounting();
	$id = Accounting::add_account($title,$desc,$type,$parent);
	if(is_null($id)){
		watchdog("accounting","Unable to add account: %title",
			array("%title"=>$title),WATCHDOG_ERROR);
		drupal_set_message(t("Unable to add account: %title",
			array("%title"=>$title)),"error");
	} else {
		drupal_set_message(t("Successfully created account: %title",
			array("%title"=>$title)));
	}
}